package com.moonshineproject.plugin.actionscript.as3parser
{
	import __AS3__.vec.Vector;
	
	import com.moonshineproject.plugin.actionscript.as3parser.completers.AccessLevels;
	import com.moonshineproject.plugin.actionscript.as3parser.vo.ClassVO;
	import com.moonshineproject.plugin.actionscript.as3parser.vo.MemberBase;
	import com.moonshineproject.plugin.actionscript.as3parser.vo.MutatorVO;
	import com.moonshineproject.plugin.actionscript.as3parser.vo.PackageVO;
	
	import flash.filesystem.File;
	
	public class MetadataManager
	{
		protected var sourceDirectories:Vector.<File>;
		protected var dot:RegExp = /\./g;
		
		
		public var globalPackageCache:PackageVO;
		
		
		public function MetadataManager(sourceDirs:Vector.<File>)
		{
			this.sourceDirectories = sourceDirs;
			
			globalPackageCache = new PackageVO();
			globalPackageCache.name = "";
		}

		public function listInstanceMembersFor(className:String, accessLevel:uint):Vector.<MemberBase>
		{
			var clazz:ClassVO = globalPackageCache.getClass(className); 
			if (!clazz) return null;
		
			var toRet:Vector.<MemberBase>;
		
			// If we extend something recurse down
			if (clazz.extendsClass)
			{
				// If we get 'private' we list everything in that class
				//  but only protected+public ones in base classes
				var baseAccess:int = (accessLevel == AccessLevels.PRIVATE) ? AccessLevels.PROTECTED : AccessLevels.PUBLIC;
				toRet = listInstanceMembersFor(clazz.extendsClass, baseAccess);
			}
			
			if (!toRet) toRet = new Vector.<MemberBase>();
			
			// Move properties over to list
			toRet = toRet.concat( clazz.getInstanceMembers(accessLevel) );
			
			return toRet;
		}

		public function getTypeOf(className:String, property:String):String
		{
			var clazz:ClassVO = globalPackageCache.getClass(className); 
			if (!clazz) return null;
			
			// Get the property we're looking for
			var prop:MemberBase = clazz.getMember(property); 
			
			var toRet:String;
			
			// If we got a prop, get it's name				
			if (prop) toRet = prop.getFullyQualifiedName();
				
			
			// If we don't have it a base class might
			if (!toRet && clazz.extendsClass)
			{
				toRet = getTypeOf(clazz.extendsClass, property);
			}
			
			return toRet; 
		}
		
		protected function findFileForClass(className:String):File
		{
			var path:String = className.replace(dot, File.separator);
			
			for (var i:int = 0; i < sourceDirectories.length; i++)
			{
				var sourceDir:File = sourceDirectories[i];
				// TODO: Lookup for MXML files as well
				var file:File = sourceDir.resolvePath(path + ".as");
				
				if (file.exists) return file;
			}
			
			return null;
		}

	}
}